﻿using System;
using VegetableSale.NET.Dao;
using VegetableSale.NET.Entities;
using VegetableSale.NET.Entities.Good;

namespace VegetableSale.NET.Services;

public class ProviderService
{

    private ProviderMapper providerMapper = new ProviderMapper();

    public Result getProviderList(QueryDTO queryDTO)
    {
        ProviderPage providerPage = new ProviderPage();
        uint pageNo = queryDTO.PageNo;
        uint pageSize = queryDTO.PageSize;
        List<Provider> ProviderList = providerMapper.getProviderList(queryDTO);
        List<Provider> returnProviders = new List<Provider>();

        if (pageSize * pageNo - 1 >= ProviderList.Count)
        {
            for (uint i = pageSize * (pageNo - 1); i <= ProviderList.Count - 1; i++)
            {
                Provider provider = ProviderList[(int)i];
                returnProviders.Add(provider);
            }
        }
        else
        {
            for (uint i = pageSize * (pageNo - 1); i <= pageSize * pageNo - 1; i++)
            {
                Provider provider = ProviderList[(int)i];
                returnProviders.Add(provider);
            }
        }

        providerPage.Records = returnProviders;
        providerPage.Pages = (int)pageNo;
        providerPage.Size = pageSize;
        providerPage.Total = (uint)ProviderList.Count;

        return new Result(200, "搜索成功", providerPage);
    }

    public Result addProvider(Provider provider)
    {
        providerMapper.addProvider(provider);
        return new Result(200, "", "");
    }

    public Result deleteProvider(int id)
    {
        providerMapper.deleteProvider(id);
        return new Result(200, "", "");
    }

    public Result updateProvider(Provider provider)
    {
        providerMapper.updateProvider(provider);
        return new Result(200, "", "");
    }

    public Result allProvider()
    {
        return new Result(0, "", providerMapper.allProvider());
    }
}

